Thread: Compile error with '||' operator.

  1. #1
    Registered User
    Join Date
    Dec 2018
    Posts
    13

    Compile error with '||' operator.

    Hey. Im trying to learn some C basics, I have only some python background and these vogue compiling errors on the silliest things are killing me. Like:

    Code:
    #include <cs50.h>
    #include <stdio.h>
    
    
    int main(void){
        char s1 = get_char("Give me a char: ");
    
    
        if (s1 == 'a' || 'A'){
            printf("OK",s1);
        }
    
    
        else if (s1 == 'd' || 'D'){
            printf("OK");
        }
    }
    Console output:

    ~/workspace/C/ $ make testC2
    clang -fsanitize=signed-integer-overflow -fsanitize=undefined -ggdb3 -O0 -std=c11 -Wall -Werror -Wextra -Wno-sign-compare -Wno-unused-parameter -Wno-unused-variable -Wshadow testC2.c -lcrypt -lcs50 -lm -o testC2
    testC2.c:7:19: error: use of logical '||' with constant operand [-Werror,-Wconstant-logical-operand]
    if (s1 == 'a' || 'A')
    ^ ~~~
    testC2.c:7:19: note: use '|' for a bitwise operation
    if (s1 == 'a' || 'A')
    ^~
    |
    testC2.c:9:21: error: data argument not used by format string [-Werror,-Wformat-extra-args]
    printf("OK",s1);
    ~~~~ ^
    testC2.c:12:24: error: use of logical '||' with constant operand [-Werror,-Wconstant-logical-operand]
    else if (s1 == 'b' || 'B')
    ^ ~~~
    testC2.c:12:24: note: use '|' for a bitwise operation
    else if (s1 == 'b' || 'B')
    ^~
    |
    3 errors generated.
    make: *** [testC2] Error


    I dont want to do bitwise op, I want to do a simple OR here and idk what is wrong.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,662
    Write
    if (s1 == 'a' || s1 == 'A' )
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Dec 2018
    Posts
    13
    Crap, such a obvious mistake and somehow I didnt saw it. Thanks a lot sir.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compile error? Logic error? Syntax Error? Please help
    By Khody Afkhami in forum C Programming
    Replies: 4
    Last Post: 10-11-2014, 01:36 AM
  2. Replies: 15
    Last Post: 11-28-2011, 11:48 AM
  3. Replies: 7
    Last Post: 06-07-2011, 01:17 PM
  4. compile time error or runtime error?
    By George2 in forum C# Programming
    Replies: 3
    Last Post: 05-07-2008, 07:08 AM
  5. no match for 'operator<<' in.... Compile Problem!
    By njd in forum C++ Programming
    Replies: 5
    Last Post: 01-09-2006, 12:40 PM

Tags for this Thread